
#include <stl/set.h>
#include <stl/vector.h>
#include <standard/geometrymath.h>
#include <standard/notification.h>
#include "operator/unitobjectoperator.h"
#include "helper/terrainactiveblockhelperobject.h"
#include "editorscenemgr.h"
#include "scene/editorscene.h"
#include "terrain/terrainblock.h"
#include "terrain/terrainobject.h"
#include "ui/terrainpanel.h"
#include "command/commandmgr.h"
#include "global.h"





CUnitObjectOperator::CUnitObjectOperator( CEditorSceneMgr* pSceneMgr ):
COperator( pSceneMgr ) {
    m_eOperatorType = EOT_COUNT;
    m_bIsButtonDown = False;
    m_nButtonDownX = 0;
    m_nButtonDownY = 0;

    m_nButtonWorkX = 0;
    m_nButtonWorkY = 0;
}
CUnitObjectOperator::~CUnitObjectOperator(){

}
bool32 CUnitObjectOperator::Init(){
    //int32 _nBrushType = CTerrainPanel::GetInterface()->GetSelectTerrainBrushType();
    //int32 _nBrushSize = CTerrainPanel::GetInterface()->GetSelectTerrainBrushSize();
    //m_rTerrainActiveBlockHelperObject = NEW CTerrainActiveBlockHelperObject();
    //m_rTerrainActiveBlockHelperObject->Init( m_pSceneMgr->GetTerrainObject(), _nBrushType, _nBrushSize);
    //m_pSceneMgr->GetScene()->AddChild(m_rTerrainActiveBlockHelperObject.GetEntityData());

    //NOTIFICATION_REGISTER(SETTERRAINBRUSHTYPENOTIFICATION, 0, this, CTerrainSurfaceOperator::OnNotification_SetTerrainBrushType);
    return True;
}
bool32 CUnitObjectOperator::Final(){
    //m_pSceneMgr->GetScene()->RemoveChild(m_rTerrainActiveBlockHelperObject.GetEntityData());
    //m_rTerrainActiveBlockHelperObject.Detach();
    //NOTIFICATION_REMOVE_DELEGATE(SETTERRAINBRUSHTYPENOTIFICATION, this, CTerrainSurfaceOperator::OnNotification_SetTerrainBrushType);
    return True;
}
bool32 CUnitObjectOperator::OnLMouseDown( int32 x, int32 y ){
    STerrainSelectedPosInfo _sTerrainSelectedPosInfo;
    CTerrainBlock* _pTerrainBlock = m_pSceneMgr->GetSelectBlock(x, y, &_sTerrainSelectedPosInfo );
    if (_pTerrainBlock) {
        SSETSTATUSCOORDTEXT _sSETSTATUSCOORDTEXT;
        _sSETSTATUSCOORDTEXT.m_fPosX = _sTerrainSelectedPosInfo.m_vPos[0];
        _sSETSTATUSCOORDTEXT.m_fPosY = _sTerrainSelectedPosInfo.m_vPos[1];
        _sSETSTATUSCOORDTEXT.m_fPosZ = _sTerrainSelectedPosInfo.m_vPos[2];
        _sSETSTATUSCOORDTEXT.m_nCellX = _sTerrainSelectedPosInfo.m_nCellX;
        _sSETSTATUSCOORDTEXT.m_nCellY = _sTerrainSelectedPosInfo.m_nCellY;

        NOTIFICATION_SEND(SETSTATUSCOORDTEXT, 0, 0, &_sSETSTATUSCOORDTEXT);
        int32 _nBrushSize = CTerrainPanel::GetInterface()->GetSelectTerrainBrushSize();
        //int32 _nSelectTerrainHeightType = CTerrainPanel::GetInterface()->GetSelectTerrainHeightType();
        //list<CTerrainBlock*>* _pvTerrainBlockList = m_rTerrainActiveBlockHelperObject->UpdateActiveBlock( _sTerrainSelectedPosInfo.m_nCellX, _sTerrainSelectedPosInfo.m_nCellY, _nBrushSize);
        m_bIsButtonDown = True;
        m_nButtonWorkX = x;
        m_nButtonWorkY = y;
        m_nButtonDownX = x;
        m_nButtonDownY = y;

        //m_vTerrainCellHeightInfoList.clear();
        return True;
    }
    return False;
}
bool32 CUnitObjectOperator::OnMouseMove( int32 x, int32 y ){
    STerrainSelectedPosInfo _sTerrainSelectedPosInfo;
    CTerrainBlock* _pTerrainBlock = m_pSceneMgr->GetSelectBlock(x, y, &_sTerrainSelectedPosInfo );
    if (_pTerrainBlock) {
        //SSETSTATUSCOORDTEXT _sSETSTATUSCOORDTEXT;
        //_sSETSTATUSCOORDTEXT.m_fPosX = _sTerrainSelectedPosInfo.m_vPos[0];
        //_sSETSTATUSCOORDTEXT.m_fPosY = _sTerrainSelectedPosInfo.m_vPos[1];
        //_sSETSTATUSCOORDTEXT.m_fPosZ = _sTerrainSelectedPosInfo.m_vPos[2];
        //_sSETSTATUSCOORDTEXT.m_nCellX = _sTerrainSelectedPosInfo.m_nCellX;
        //_sSETSTATUSCOORDTEXT.m_nCellY = _sTerrainSelectedPosInfo.m_nCellY;
        //NOTIFICATION_SEND(SETSTATUSCOORDTEXT, 0, 0, &_sSETSTATUSCOORDTEXT );
        //int32 _nBrushSize = CTerrainPanel::GetInterface()->GetSelectTerrainBrushSize();
        //list<CTerrainBlock*>* _pvTerrainBlockList = m_rTerrainActiveBlockHelperObject->UpdateActiveBlock( _sTerrainSelectedPosInfo.m_nCellX, _sTerrainSelectedPosInfo.m_nCellY, _nBrushSize);
        //if (m_bIsButtonDown) {
        //    if (m_nButtonWorkX != x || m_nButtonWorkY != y) {
        //        _Work(_sTerrainSelectedPosInfo.m_nCellX, _sTerrainSelectedPosInfo.m_nCellY, _nBrushSize);
        //        m_nButtonWorkX = x;
        //        m_nButtonWorkY = y;
        //        return True;
        //    }
        //}
    }
    return False;
}
bool32 CUnitObjectOperator::OnLMouseUp( int32 x, int32 y ){
    bool32 _bResult = False;
    if (m_bIsButtonDown) {
        if (m_nButtonDownX == x || m_nButtonDownY == y) {
            STerrainSelectedPosInfo _sTerrainSelectedPosInfo;
            CTerrainBlock* _pTerrainBlock = m_pSceneMgr->GetSelectBlock(x, y, &_sTerrainSelectedPosInfo);
            if (_pTerrainBlock) {
                int32 _nBrushSize = CTerrainPanel::GetInterface()->GetSelectTerrainBrushSize();
                _Work(_sTerrainSelectedPosInfo.m_nCellX, _sTerrainSelectedPosInfo.m_nCellY, _nBrushSize);
                _bResult = True;
            }
        }

        _FinishedWork();
    }
    m_bIsButtonDown = False;
    m_nButtonDownX = 0;
    m_nButtonDownY = 0;

    m_nButtonWorkX = 0;
    m_nButtonWorkY = 0;
    return _bResult;
}
void CUnitObjectOperator::_Work( int32 nCellX, int32 nCellY, int32 nBrushSize ){


}
void CUnitObjectOperator::OnNotification_SetTerrainBrushType( int32 w, int32 l, void* pUserData ){
    //m_rTerrainActiveBlockHelperObject->SetBrushInfo(w, l);
}
//void CSceneCorationOperator::_AddTerrainCellHeightInfo( int32 x, int32 y, float fValue ){
//    //uint32 _nKey = MAKELONG( y, x );
//    //if(m_vTerrainCellHeightInfoList.find( _nKey ) == m_vTerrainCellHeightInfoList.end()){
//    //    TV2 _sVert;
//    //    _sVert.X = fValue;
//    //    _sVert.Y = 0.0f;
//    //    m_vTerrainCellHeightInfoList.insert( _nKey, _sVert );
//    //}
//}
void CUnitObjectOperator::_FinishedWork(){
    //CTerrainObject* _pTerrainObject = m_pSceneMgr->GetTerrainObject();
    //map< uint32, TV2 >::iterator it = m_vTerrainCellHeightInfoList.begin();
    //map< uint32, TV2 >::iterator it_end = m_vTerrainCellHeightInfoList.end();
    //for(; it != it_end; it++){
    //    uint32 _nKey = it.first();
    //    int32 _nX = HIWORD( _nKey );
    //    int32 _nY = LOWORD( _nKey );
    //    it.second().Y = _pTerrainObject->GetCellHeight( _nX, _nY );
    //}

    //CTerrainHeightCmd* _pTerrainHeightCmd = NEW CTerrainHeightCmd( &m_vTerrainCellHeightInfoList );
    //m_pSceneMgr->GetCommandMgr()->AddCommand( _pTerrainHeightCmd );
    //m_vTerrainCellHeightInfoList.clear();
}


